package com.bw.lib_log.logger

import com.bw.lib_log.LoggerLevel
import com.bw.lib_log.LoggerStrategy

class Logger constructor(
    //主构造
    private val TAG:String,
    private val level: LoggerLevel,
    private val strategy: LoggerStrategy,
    private val isDebug: Boolean,
    private val saveUrl: String
){
    //次构造继承自主构造
    constructor(builder: Builder):this(
        builder.TAG,
        builder.level,
        builder.strategy,
        builder.isDebug,
        builder.saveUrl
    )


    /**
     * 策略者模式: 根据不同的策略创建不同的logger对象
     */
    private var logger: ILogger//日志logger

    init {
        when(strategy){
            LoggerStrategy.LOG_CAT -> logger = LogCatLogger()
            LoggerStrategy.FILE -> logger = FileLogger()
            LoggerStrategy.NET -> logger = NetLogger()
        }
        logger.setDebug(isDebug)
        logger.setLevel(level)
        logger.setTag(TAG)
        logger.setSavaUrl(saveUrl)
    }
    //4 打印函数
    /**
     * 输出Debug log
     */
    fun d(Tag:String,log:String){
        logger.d(Tag,log)
    }

    /**
     * 输出Warn log
     */
    fun w(Tag:String,log:String){
        logger.w(Tag,log)
    }

    /**
     * 输出Verbose log
     */
    fun v(Tag:String,log:String){
        logger.v(Tag,log)
    }

    /**
     * 输出Error log
     */
    fun e(Tag:String,log:String){
        logger.e(Tag,log)
    }
    /**
     * 输出Info log
     */
    fun i(Tag:String,log:String){
        logger.i(Tag,log)
    }

    /**
     * 构建者设计模式:多个参数的时候,按需设置参数,不需要全部设置
     */
    class Builder {
        var TAG:String = "默认TAG"
        var level: LoggerLevel = LoggerLevel.DEBUG
        var strategy: LoggerStrategy = LoggerStrategy.LOG_CAT
        var isDebug: Boolean = true
        var saveUrl: String = "默认路径"

        fun setTAG(TAG: String): Builder{
            this.TAG = TAG
            return this
        }
        fun setLevel(level: LoggerLevel): Builder{
            this.level = level
            return this
        }
        fun setStrategy(strategy: LoggerStrategy): Builder{
            this.strategy = strategy
            return this
        }
        fun setDebug(isDebug: Boolean): Builder{
            this.isDebug = isDebug
            return this
        }
        fun setSaveUrl(saveUrl: String): Builder{
            this.saveUrl = saveUrl
            return this
        }
        fun build(): Logger {
            return Logger(this)
        }
    }
}